<?php
class ViSeminarTag_Controller extends AdminController {

    public $DISPLAY_VISEMINAR_LIST_UUID = 'admin/viSeminarTag/display/viSeminarList';

    public function __construct() {
        parent::__construct('viSeminarTag');
        $this->setDefaultListOrder('id');
    }

    function all() {
        $this->initPagination('vi_seminar_tag');

        $this->viewData['viSeminarTagList'] = $this->dao->queryAll(
                $this->orderLimitAndOffset('SELECT * FROM vi_seminar_tag'));

        $this->renderSubview('admin/viSeminarTag/viSeminarTagList');
    }

    function display() {
        $id = $this->input->get('id');

        $viSeminarTag = $this->dao->getById('vi_seminar_tag', $id);
        if (! $viSeminarTag) {
            $this->redirectToList('error.recordNotFound');
        }
        $this->viewData['viSeminarTag'] = $viSeminarTag;

        // Read all seminars having this tag
        $this->viewData['viSeminarList'] = $this->dao->queryAll(sql::orderBy($this->DISPLAY_PERSON_LIST_UUID,
                'SELECT s.* FROM vi_seminar s JOIN vi_seminar_tag_asoc a ON a.vi_seminar_id = s.id WHERE a.vi_seminar_tag_id = :id',
                'begin_date'),
                array('id' => $id));

        $this->renderSubview('admin/viSeminarTag/viSeminarTagDisplay');
    }

    function edit() {
        $id = $this->input->get('id');
        $viSeminarTag = array();

        if (valid::isId($id)) {
            $viSeminarTag = $this->dao->getById('vi_seminar_tag', $id);
            if (! $viSeminarTag) {
                $this->msg->error('error.recordNotFound');
                $this->redirect('/admin/viSeminarTag/all');
            }
        }
        else {
            // Set default values for a new record here
        }

        $this->loadFormData();

        $this->viewData['viSeminarTag'] = $viSeminarTag;
        $this->renderSubview('admin/viSeminarTag/viSeminarTagEdit');
    }

    protected function loadFormData() {
    }

    function save() {
        $viSeminarTag = $this->postToDbValues();
        $id = $viSeminarTag['id'];
        $this->viewData['viSeminarTag'] = $viSeminarTag;

        // Basic validation
        $validator = new EntityValidator($viSeminarTag, 'viSeminarTag', $this->msg);
        $validator->required('name');

        if ($this->msg->hasErrors()) {
            $this->loadFormData();
            $this->publishErrors('admin/viSeminarTag/viSeminarTagEdit');
            return;
        }

        // Database validation - unique name
        if ($this->dao->alreadyExists('vi_seminar_tag', 'name', $viSeminarTag, 'id')) {
            $this->msg->error('viSeminarTag.error-alreadyExists', $viSeminarTag['name']);
            $this->loadFormData();
            $this->publishErrors('admin/viSeminarTag/viSeminarTagEdit');
            return;
        }

        // Insert or update
        if (sql::isPersistent($viSeminarTag)) {
            $this->dao->update('vi_seminar_tag', $viSeminarTag);
            $this->msg->info('viSeminarTag.editSuccess', $viSeminarTag['name']);
        }
        else {
            $this->dao->insert('vi_seminar_tag', $viSeminarTag);
            $this->msg->info('viSeminarTag.createSuccess', $viSeminarTag['name']);
        }

        $this->ajaxSuccessOrRedirect('/admin/viSeminarTag/all', $viSeminarTag);
    }

    function delete() {
        $id = $this->input->get('id');

        if (! valid::isId($id)) {
            $this->msg->error('error.invalidId');
            $this->redirect('/admin/viSeminarTag/all');
        }

        // Basic validation
        $viSeminarTag = $this->dao->getById('vi_seminar_tag', $id);
        if (! $viSeminarTag) {
            $this->msg->error('error.recordNotFound');
            $this->redirect('/admin/viSeminarTag/all');
        }

        $this->dao->delete('vi_seminar_tag', $id);

        $this->msg->error('viSeminarTag.deleteSuccess', $viSeminarTag['name']);
        $this->redirect('/admin/viSeminarTag/all');
    }
}
?>
